package com.lemon.components.ant;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

import com.lemon.components.ant.beans.ProjectInfo;
import com.lemon.components.ant.util.ConfigUtil;
import com.lemon.ui.codemaker.component.RecentlyFile;

public class MavenService {

	public void mvninstall() {
		ProjectInfo projectInfo = ConfigUtil.getInstance(RecentlyFile.configFilePath).getProjectInfo();
		try {
			build("mvn clean install",projectInfo.getProjectPath()+"/"+projectInfo.getArtifactId()+"/");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void build(String cmd,String dir) throws Exception {
		Process p = Runtime.getRuntime().exec("cmd /c "+cmd, null, new File(dir));
		BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream(),"gbk"), 1024);
		String tmp;
		while((tmp = br.readLine())!=null){
			System.out.println(tmp);
		}
		p.waitFor();
		p.destroy();
		killProcess();
	}
	
	public void killProcess() {
        Runtime rt = Runtime.getRuntime();
        try {
            rt.exec("cmd.exe /C start wmic process where name='cmd.exe' call terminate");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
